<template>

    <UxDialog :visible="show" @close="close()">

        <div class="woo-setting-agreen" v-if="!agreen">
            <figure><img src="https://img.podpartner.com/static/images-20230202/woo-setting.png" alt=""></figure>
            <p>Please make sure the configuration here is identical to your WooCommerce settings; otherwise, the integration may not work.</p>

            <button type="primary" @click="agreen = true">Confirm</button>
            <button @click="close()">Cancel</button>
        </div>

        <div class="woo-setting-form" v-loading="loading" v-if="agreen">

            <figure>
                <img src="@/assets/images/setting/dialog-woo.png" alt="">
            </figure>
            <h2>WooCommerce</h2>
            <p>
                <strong>Your store's web address, Type in your store URL</strong>
                <label><input @focus="$event.currentTarget.select()" type="text" v-model="shop_url" /></label>
            </p>
            <p>
                <strong>Consumer key</strong>
                <label><input @focus="$event.currentTarget.select()" type="text" v-model="consumer_key" /></label>
            </p>
            <p>
                <strong>Consumer secret</strong>
                <label><input @focus="$event.currentTarget.select()" type="text" v-model="consumer_secret" /></label>
            </p>

            <div class="woo-setting-form-action">
                <button @click="close()">Cancel</button>
                <button type="primary" @click="save()">Save</button>
            </div>

        </div>
    </UxDialog>

</template>

<script>
export default {
    props: {
        value: { type: Object, default: () => undefined },
    },

    data () {
        return {
            show: false,
            loading: false,
            agreen: false,
            shop_url: '',
            consumer_key: '',
            consumer_secret: '',
        };
    },
    watch: {
        value (newval, oldval) {

            newval && this.open()

        }
    },
    methods: {

        save () {

            const request = {
                shop_url: this.shop_url,
                consumer_key: this.consumer_key,
                consumer_secret: this.consumer_secret,
            }
            this.loading = true;
            this.$http('UserStore.setWooSetting/' + this.id, request).then(response => {

                if (response.code === 0) {
                    this.value.shop_url = this.shop_url;
                    this.value.consumer_key = this.consumer_key;
                    this.value.consumer_secret = this.consumer_secret;
                    this.close();
                    this.$uxMessage.success("successfully to update store setting");
                } else {
                    this.loading = false;
                    this.$uxMessage.error(response.msg || "Failed to update shop");
                }
            })

        },
        open () {
            this.show = true;
            this.agreen = false;
            this.loading = false;
            this.id = this.value.id;
            this.shop_url = this.value.shop_url;
            this.consumer_key = this.value.consumer_key;
            this.consumer_secret = this.value.consumer_secret;
        },


        close () {
            this.show = false;
            this.$emit('input', null)
        }

    },
    mounted () { },
};
</script>

<style lang="scss" scoped>
.woo-setting-agreen {
    width: 320px;
    height: 414px;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    padding: 0px 14px 14px;

    figure {
        margin: 40px auto 0;
        width: 80px;
        height: 80px;
        img {
            width: 100%;
            height: 100%;
        }
    }

    p {
        flex: 1;
        margin: 32px 0 0;
        padding: 10px 0;
        font-size: 16px;
        color: #000000;
        line-height: 20px;
        text-align: center;
    }

    button {
        margin-top: 24px;

        & + button {
            margin-top: 8px;
        }
    }
}

.woo-setting-form {
    width: 468px;
    box-sizing: border-box;
    padding: 32px 24px 32px;
    display: flex;
    flex-direction: column;

    figure {
        width: 60px;
        height: 60px;
        margin: 0 auto 0;

        svg,
        img {
            width: 100%;
            height: 100%;
        }
    }

    h2 {
        margin-top: 14px;
        margin-bottom: 6px;
        font-size: 18px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #000000;
        line-height: 20px;
        text-align: center;
    }

    p {
        margin-top: 26px;
        strong {
            display: block;
            font-size: 14px;
            color: #555555;
            line-height: 16px;
        }

        label {
            display: block;
            margin-top: 8px;
        }
    }

    .woo-setting-form-action {
        display: flex;
        margin: 32px -6px 0;
        button {
            flex: 1;
            margin: 0 6px;
        }
    }
}
</style>
